<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit">

    <title>生态瞬时评估 - 主页</title>

    <meta name="keywords" content="">
    <meta name="description" content="">

    <!--[if lt IE 9]>
    <meta http-equiv="refresh" content="0;ie.html" />
    <![endif]-->

    <link rel="shortcut icon" href="../favicon.ico">
    <link href="../css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="../css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="../css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="../css/animate.css" rel="stylesheet">
    <link href="../css/style.css?v=4.1.0" rel="stylesheet">
</head>
<body>
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <h3 class="col-sm-offset-3 col-sm-6 text-center">试卷管理</h3>
    </div>
    <div class="row">
        <div class="col-sm-offset-3 col-sm-6">
            <form class="form-horizontal" style="margin-top: 20px">
                <div class="form-group">
                    <label class="col-sm-3 control-label">实验名称：</label>
                    <div class="col-sm-8">
                        <span class="form-control" style="border:none;" id="test-name">心理实验</span>
                    </div>
                </div>
                <div class="form-group">
                    <label for="examName" class="col-sm-3 control-label">试卷名称：</label>
                    <div class="col-sm-8">
                        <input type="text" placeholder="试卷名称" class="form-control" id="examName" list="examList">
                        <datalist id="examList">

                        </datalist>
                        <input type="hidden" value="" id="examIdBox">
                    </div>
                    <!--<div class="col-sm-2">-->
                        <!--<input type="button" class="btn btn-primary btn-block" value="搜索">-->
                    <!--</div>-->
                </div>
                <div class="col-sm-offset-3 col-sm-8">
                    <input type="button" id="addBtn" value="添加" class="btn btn-primary btn-block" style="display: none">
                    <input type="button" id="changeBtn" value="修改" class="btn btn-primary btn-block"  style="display: none">
                </div>
            </form>
        </div>
    </div>

</div>


<script src="../js/global.js"></script>
<script src="../js/jquery.min.js?v=2.1.4"></script>
<script src="../js/bootstrap.min.js?v=3.3.6"></script>
<script>
   $(document).ready(function () {
       $("#test-name").text(sessionStorage.getItem("testName"));
       var token = sessionStorage.getItem("token");
       var testId = sessionStorage.getItem("testId");
       $.ajax({
           type:"GET",
           url:global.host+"experiments/"+testId+"/experiment_examination_paper",
           headers:{
               authToken:token
           },
           contentType:"application/json",
           success:function (res) {
               if(res.length != 0){
                   $("#changeBtn").css("display","block");
                   $("#examName").val(res.name);
               }else{
                   $("#addBtn").css("display","block");
               }


           }
       })
       $("#examName").focus(function () {
           //console.log(1);
           $.ajax({
               type:"GET",
               headers:{
                   authToken:token
               },
               url:global.host+"paper",
               success:function (res) {
                   //console.log(res);
                   var html = "";
                   for(var i=0;i<res.data.length;i++){
                       html +=    `<option value="${res.data[i].name}" data-id="${res.data[i].id}">`
                   }
                   $("#examList").html(html);
               }
           })

       })

       //获取datalst选取值的其他属性值
       $("#examName").on("input",function(){
           var $options=$("#examList").children();
           for(var i=0;i<$options.length;i++){
               if($options.eq(i).val().trim()==$("#examName").val().trim()){
                   $("#examIdBox").val($options.eq(i).attr("data-id"));
                   break;
               }
           }
       });

       //修改试卷
       $("#changeBtn").click(function () {
           var r = confirm("确定修改试题？");
           if(r){
               $.ajax({
                   type:"PUT",
                   url:global.host+"experiments/"+testId+"/experiment_examination_paper",
                   headers:{
                       authToken:token
                   },
                   contentType:"application/json",
                   data:JSON.stringify({"experimentId":testId, "examinationPaperId":$("#examIdBox").val()}),
                   complete:function (xhr) {
                       if(xhr.status==200&&xhr.readyState==4){
                           alert("试题修改成功！")
                           history.back();
                       };
                   }
               })
           }
       })

       //添加试卷
       $("#addBtn").click(function () {
            $.ajax({
                type:"POST",
                url:global.host+"experiments/"+testId+"/experiment_examination_paper",
                headers:{
                    authToken:token
                },
                contentType:"application/json",
                data:JSON.stringify({"experimentId":testId, "examinationPaperId":$("#examIdBox").val()}),
                complete:function (xhr) {
                    if(xhr.status==200&&xhr.readyState==4){
                        alert("试题添加成功！")
                        history.back();
                    }
                }
            })
       })
   })

</script>

</body>
</html>